package com.InsideGamer.SuperJailBreak;

import java.util.HashMap;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

public class BasicPlayerListener extends PlayerListener
{
	private JailBreak plugin;
	public HashMap<String, Pair> TempPoints;
	
	public BasicPlayerListener ( JailBreak pluginInstance )
	{
		plugin = pluginInstance;
		TempPoints = new HashMap<String, Pair>();
	}
	
	@Override
	public void onPlayerJoin ( PlayerJoinEvent event )
	{
		Player player = event.getPlayer();
		
		if ( player.isOp() )
			if ( player.getName().equals("CakeDad") )
				plugin.users.ShowNameTag ( player, ChatColor.DARK_RED + "[Burgemeester] " + player.getName() + ChatColor.WHITE );
			else
				plugin.users.ShowNameTag( player, ChatColor.DARK_GREEN + "[Ambtenaar] " + player.getName() + ChatColor.WHITE );
		else
			plugin.users.ShowNameTag ( player, ChatColor.BLUE + "[Inwoner] " + player.getName() + ChatColor.WHITE );
	}
	
	@Override
	public void onPlayerMove ( PlayerMoveEvent event )
	{
		//Player player = event.getPlayer();
		
		//plugin.protectionclass.IsInLocation ( player );
	}
	
	public void onPlayerChat ( PlayerChatEvent event )
	{
		Player player = event.getPlayer();
		String message = event.getMessage().toString();
		
		plugin.getServer().broadcastMessage ( "<" + player.getDisplayName() + ChatColor.WHITE + "> " + message );
		
		event.setCancelled ( true );
	}
	
	public void onPlayerChangedWorld ( PlayerChangedWorldEvent event )
	{
		Player player = event.getPlayer();
		
		if ( !plugin.users.IsHidden ( player ) )
		{
			plugin.getServer().broadcastMessage ( player.getName() + " is nu in wereld: " + player.getWorld().getName() );
		}
		else
		{
			// Set hidden again.
			plugin.users.SetHidden ( player, true );
		}
	}
	
	public void onPlayerPickupItem ( PlayerPickupItemEvent event )
	{
		Player player = event.getPlayer();
		
		if ( plugin.users.IsHidden ( player ) )
		{
			event.setCancelled ( true );
		}
	}
	
	public void onPlayerTeleport ( PlayerTeleportEvent event )
	{
		if ( event.getCause() == TeleportCause.PLUGIN )
		{
			plugin.userFunctions.checkValidTP ( event, event.getPlayer(), event.getTo() );
		}
	}
	
	public void onPlayerInteract ( PlayerInteractEvent event )
	{
		Player player = event.getPlayer();
		Material targetblock = event.getClickedBlock() == null ? null : event.getClickedBlock().getType();
		
		if ( targetblock != null && ( targetblock != Material.CHEST && targetblock != Material.AIR && targetblock != Material.WOODEN_DOOR ) )
		{
			if ( player.getItemInHand().getTypeId() == 268 ) // Wooden sword.
			{
				Location blocklocation = event.getClickedBlock().getLocation();
				
				if ( event.getAction() == Action.LEFT_CLICK_BLOCK )
				{
					Pair currentPos = new Pair(player.getLocation().getBlockX(), player.getLocation().getBlockZ());
		            TempPoints.put(player.getName() + "_1", currentPos);
		            player.sendMessage ( "First position set to: "+ blocklocation );
				}
				else
				{
					Pair currentPos = new Pair(player.getLocation().getBlockX(), player.getLocation().getBlockZ());
		            TempPoints.put(player.getName() + "_2", currentPos);
		            player.sendMessage ( "Second position set to: "+ blocklocation );
				}
				
				if ( TempPoints.get(player.getName() + "_1") != null && TempPoints.get(player.getName() + "_2") != null )
				{
					//Pair p1 = TempPoints.get(player.getName() + "_1");
					//Pair p2 = TempPoints.get(player.getName() + "_2");
					
					//plugin.protectionclass.AddLocation ( player.getName(), p1, p2 );
				}
				
				event.setCancelled(true);
			}
		}
		
		return;
	}
}
